﻿package 
{

	
	import flash.display.Sprite;
	import flash.events.*;
	import caurina.transitions.*;
	import gemini.utils.*;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	import code.events.GeminiEvent;

	public class Tutorials extends Sprite
	{
		private var _mainframe;
		protected  var _config:Object={};
		private var _path:String="";
		private var tutorials:Array=[];
		private var _container:Sprite
		private var _step;
		private var _tut:Object;
		private var _mouse;
		private var _dialog;
		/*
		*
		*
		*/
		private var _tutMethod;
		private var _object;
		private var _objectMethod;
		private var _objectMethodParams;
		private var _tempValue;
		private var target;
		public function Tutorials()
		{	
			super();
			_mouse=new tutmouse();
			_mouse.x=300;
			_mouse.y=300;
			//
			_dialog=new tutorialdialog();
			_dialog.x=500;
			_dialog.y=300;
			//init();	
		}
		public function get mainframe()
		{
			return _mainframe;	
		}
		public function set mainframe(mf)
		{
			trace("[tutorial] mainframe set "+mf);
			_mainframe=mf;
			init();
			
		}
		private function init()
		{
			
			var ld:LoadJSONs=new LoadJSONs();
			ld.addFile(_path+"php/tutorials.php",_config,"tutorials");
			ld.onComplete(tutorialsReady);
			ld.loadFiles();
		
		}
		private function tutorialsReady(e)
		{
			_container=new Sprite();
			addChild(_container);
			for(var n=0;n<_config.tutorials.length;n++)
			{
				tutorials[n]=new tutorialclip();
				tutorials[n].btn.addEventListener(MouseEvent.MOUSE_DOWN,startTutorial);
				tutorials[n].txt.text=_config.tutorials[n]._name;
				tutorials[n].myNum=n;
				tutorials[n].x=100;
				tutorials[n].y=30+n*tutorials[n].height;
				_container.addChild(tutorials[n]);
			}
			
		}
		private function startTutorial(e)
		{
			_tempValue=null;
			reset();
			addMouse();
			addChild(_dialog);
			_step=0;
			_tut=_config.tutorials[e.target.parent.myNum];
			trace("[tutorial] Starting "+_tut._name);
			nextStep();
		}
		private function addMouse()
		{
			
			_mainframe.fixed.addChild(_mouse);
		}
		private function nextStep(e=null)
		{
			btn.removeEventListener(MouseEvent.MOUSE_DOWN,nextStep);
			addToLog("doing step "+_step);
			_object=_tut._actions[_step]._action._object;
			_objectMethod=_tut._actions[_step]._action._objectMethod;	
			_objectMethodParams=_tut._actions[_step]._action._params;
			_tutMethod=_tut._actions[_step]._action._tutorialMethod;
		
			/*
			*
			*
			*/
			if(_tut._actions[_step]._title!="")
			{
				_dialog.title_txt.text=_tut._actions[_step]._title;
			}
			if(_tut._actions[_step]._desc!="")
			{				
				_dialog.dialog_txt.text=_tut._actions[_step]._desc;
			}
			
			
			/*
			*
			*/
			target=_mainframe[_object];
			target.addEventListener(GeminiEvent.GEMINI_UPDATE,this[_tutMethod]);
			addToLog("Object method is "+_objectMethod);		
		
			if(_objectMethodParams=="savedValue")
			{
				target[_objectMethod](_tempValue);
			}
			else
			{
				target[_objectMethod](_objectMethodParams);
			}
			
			
		}
		/*
		*
		*
		*
		*
		*/
		private function moveMouse(e)
		{
			addToLog("move mouse method to ");
			var _target=e.params.getChildAt(0);
			var point=new Point();
			point.x=_target.x+5;
			point.y=_target.y+5;
			point=_target.parent.localToGlobal(point);
			
			Tweener.addTween
			(
				_mouse,
				{
					x:point.x,
					y:point.y,
					time:.5,
					transition:"easeInOutQuad",
					onComplete:makeDelay
				}
			
			)
			/*Tweener.addTween
				(
					_dialog,
					{
						x:point.x,
						y:point.y,
						time:.5,
						transition:"easeInOutQuad"
						
					}
					
				)*/
		}
		private function idle(e)
		{
			
			makeDelay();
		}
		private function saveValue(e)
		{
				
				_tempValue=e.params.value;
				trace("TEMP VALUE "+_tempValue);
				makeDelay();
			
		}
		private function click(e)
		{
			_mouse.gotoAndPlay("click");
			makeDelay();
		}
		
		/*
		*
		*
		*
		*
		*/
		private function makeDelay()
		{
			
			
			target.removeEventListener(GeminiEvent.GEMINI_UPDATE,this[_tutMethod]);
			addToLog("DELAYED for "+_tut._actions[_step]._action._delay);
			_step++;	
			if(_step!=_tut._actions.length)
			{
				var to=setTimeout(nextStep,1000);
			}
			else
			{
				trace("close tutorial");
				Tweener.addTween
					(
						_mouse,
						{
							x:300,
							y:300,
							time:.5,
							transition:"easeInOutQuad"
							
						}						
					)
					removeChild(_dialog);
			}
			
			//btn.addEventListener(MouseEvent.MOUSE_DOWN,nextStep);
			
			
		}
		private function addToLog(t)
		{
			_mainframe.txt.text+=t+"\n";	
		}
		protected function reset()
		{
			try
			{
				_mainframe.fixed.removeChild(_mouse);
				
			}
			catch(e)
			{
				
			}
		}
	}
}